package com.zHan.sky.controller.User;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zHan.sky.common.content.BaseContext;
import com.zHan.sky.common.result.Result;
import com.zHan.sky.entity.AddressBook;
import com.zHan.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增地址")
    public Result<String> save(@RequestBody AddressBook addressBook) {
        //获取当前登录用户的id
        addressBook.setUserId(BaseContext.getCurrentId());
        //执行方法
        addressBookService.save(addressBook);
        return Result.success("新增地址成功");
    }

    /**
     * 查询所有地址
     *
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有地址")
    public Result<List<AddressBook>> list(AddressBook addressBook) {
        //获取当前登录用户的id
        addressBook.setUserId(BaseContext.getCurrentId());
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        //执行方法
        List<AddressBook> addressBookList = addressBookService.list(queryWrapper);
        return Result.success(addressBookList);
    }

    /**
     * 根据id查询单个地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询单个地址")
    public Result<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return Result.success(addressBook);
        }
        return Result.error("没有找到该地址");
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改地址")
    public Result<String> update(@RequestBody AddressBook addressBook) {
        if (addressBook != null) {
            addressBookService.updateById(addressBook);
            return Result.success("修改地址成功");
        }
        return Result.error("修改地址失败");
    }

    /**
     * 删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除地址")
    public Result<String> delete(@RequestParam Long ids) {
        if (ids != null) {
            addressBookService.removeById(ids);
            return Result.success("删除地址成功");
        }
        return Result.error("删除地址成功");
    }

    /**
     * 更新默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation(value = "更新默认地址")
    public Result<String> updateDefault(@RequestBody AddressBook addressBook) {
        //条件构造器
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        //先把所有的地址改成非默认地址
        updateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        updateWrapper.set(AddressBook::getIsDefault, 0);
        //执行方法
        addressBookService.update(updateWrapper);
        //设置默认地址
        addressBook.setIsDefault(1);
        //执行更新方法
        addressBookService.updateById(addressBook);
        return Result.success("更新默认地址成功");
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    @ApiOperation(value = "查询默认地址")
    public Result<AddressBook> getDefault() {
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault, 1);
        //执行查询方法
        AddressBook addressBookOne = addressBookService.getOne(queryWrapper);
        if (addressBookOne != null) {
            return Result.success(addressBookOne);
        }
        return Result.error("查询默认地址失败");
    }
}
